<template>
  <div class="Receive">
    <van-address-list
      v-if="flag"
      v-model="chosenAddressId"
      :list="receives"
      :disabled-list="disabledList"
      disabled-text="以下地址超出配送范围"
      default-tag-text="默认"
      @add="onAdd"
      @edit="onEdit"
    />
    <van-address-edit
      v-else
      :area-list="areaList"
      show-postal
      show-delete
      show-set-default
      show-search-result
      :search-result="searchResult"
      :area-columns-placeholder="['请选择', '请选择', '请选择']"
      @save="onSave"
      @delete="onDelete"
      @change-detail="onChangeDetail"
    />
  </div>
</template>
<script>
import { Toast } from 'vant';
import { receives } from '@/api/mine';

export default {
  data() {
    return {
      receives: [],
      disabledList: [],
      flag: true,
      chosenAddressId: '201',
      areaList: {
        provice: ['北京', '天津'],
      },
      searchResult: [],
    };
  },
  created() {
    this.getreceives();
  },
  methods: {
    async getreceives() {
      const res = await receives();
      res.data.forEach((item) => {
        if (item.isSend) {
          this.receives.push(item);
        } else {
          this.disabledList.push(item);
        }
      });
    },
    onAdd() {
      this.flag = !this.flag;
    },
    onEdit(item, index) {
      Toast('编辑地址:' + index);
    },
    onSave(content) {
      this.flag = !this.flag;
      // this.receives.push(content)
      Toast('保存成功');
      console.log(content);
    },
    onDelete() {
      Toast('删除成功');
    },
    onChangeDetail(val) {
      if (val) {
        this.searchResult = [
          {
            name: '黄龙万科中心',
            address: '杭州市西湖区',
          },
        ];
      } else {
        this.searchResult = [];
      }
    },
  },
};
</script>
<style
  lang="scss"
  scoped
></style>
